package org.karelun.gef.utils;

import org.eclipse.swt.graphics.RGB;

public class ColorUtils {

	 public static String toString(RGB rgb) {
		return toString(rgb.red, rgb.green, rgb.blue);
	}

	public static String toString(int r, int g, int b) {
		int value = ((r & 0xff) << 16) | ((g & 0xff) << 8) | (b & 0xff);
		return String.format("#%06X", value); //$NON-NLS-1$
	 }

	public static RGB toRGB(String s) {
		if (s != null && s.startsWith("#") && s.length() == 7) { //$NON-NLS-1$
			s = s.substring(1).toLowerCase();
			int value = Integer.parseInt(s, 16);
			return split(value);
		}
		return null;
	}

	private static RGB split(int value) {
		int r = (value >> 16) & 0xff;
		int g = (value >> 8) & 0xff;
		int b = value & 0xff;
		return new RGB(r, g, b);
	}
}
